﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ecopsy.DataTier;
using Ecopsy.Tools;

namespace AgencyEntityLib.BaseEntity
{
    
    // Элемент расписания поверхности
    public class SurfaceSheduleItem : EntitySingle
    {
        public string SideID { get; set; }
        public int SurfNumber { get; set; }
        public DateTime DtStart { get; set; }
        public DateTime DtFinish  { get; set; }
        public string ContractId { get; set; }
        public string ContractTypeID { get; set; }

    }

    public class SideShedule : EntityList,ISqlSignature  
    {
        public static SideShedule GetSideShedule (string SideID,DateTime dtStart, DateTime dtEnd)
        {
            SideShedule res = new SideShedule() { dtStart = dtStart, dtEnd = dtEnd };
            Side s = new Side() { ID = SideID };
            s.LoadReadOnly();
            res.Side = s;
            res.Load();
            return res;
            
        }
        public DateTime dtStart { get; set; }
        public DateTime dtEnd { get; set; }
        public Side Side { get; set; }
        public string SideID { get { return Side .ID; } }

        /// <summary>
        /// Признак наличия очереди 
        /// </summary>
        [EntityNonSerialized] 
        public bool IHave_Q 
        {
            get 
            {
                return MaxSurfValue > this.Side.SurfaceCount-1;
            }
        }
        /// <summary>
        /// Максмальное значение поверъхности в очереди
        /// </summary>
        public int MaxSurfValue 
        { 
            get 
            {
                List<SurfaceSheduleItem> l = this.ToList<SurfaceSheduleItem>();
                return (l.Count > 0 ? this.ToList<SurfaceSheduleItem>().Max(w => w.SurfNumber) : 0); 
                
            } 
        } 

        /// <summary>
        ///  Вернуть SurfaceSheduleItem Если есть пересечение по периоду и номеру поверхности 
        /// </summary>
        /// <param name="DtStart"></param>
        /// <param name="DtEnd"></param>
        /// <param name="SurfaceNumber"></param>
        /// <param name="CrossTrype">Тип пересечения отрезков</param>
        /// 
        /// <returns></returns>
        public SurfaceSheduleItem GetSurfaceSheduleItem(DateTime DtStart, DateTime DtEnd, int SurfaceNumber, ref int CrossTrype)
        {
            this.Reset();
            SurfaceSheduleItem res=null;
            foreach (SurfaceSheduleItem i in this)
            {
                if (i.SurfNumber == SurfaceNumber)
                {
                    CrossTrype = Ecopsy.Tools.DateTimeTools.GetCrossPeriod(DtStart, DtEnd, i.DtStart, i.DtFinish);
                    
                    if (CrossTrype == 1||CrossTrype ==4)
                        res = i;
                }       
            }
            return res ;

        }
        public override string ItemAssemblyName
        {
            get
            {
                return "AgencyEntityLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
            }
        }
        public override string ItemAssemblyType
        {
            get
            {
                return "AgencyEntityLib.BaseEntity.SurfaceSheduleItem";
            }
        }





        #region ISqlSignature Members

        public string SaveProcedure
        {
            get { return string.Empty; }
        }

        public string DeleteProcedure
        {
            get { return string.Empty; }
        }

        public string LoadProcedure
        {
            get { return "SideSheduleLoad"; }
        }

        #endregion
    }
}
